﻿@using Model;
@{
    //  SiteSecurity.RequireAuthenticatedUser();
    // Set the layout page and page title
    Layout = "~/Admin/_AdminSiteLayout.cshtml";
    Page.Title = "Add Question Type";

    <link rel="stylesheet" type="text/css" href="../Styles/Validation.css" />

    var statusMessage = "";
    bool isValid = false;
    int i = 1;

    List<QuestionType> questionTypeList = new List<QuestionType>();

    i = 0;

    if (IsPost)
    {
        DateTime currentDate = DateTime.Now;

        questionTypeList.Clear();

        i = 1;

        while (Request.Form["QuestionType" + i] != null)
        {
            QuestionType aQuestionType = new QuestionType();

            aQuestionType.QType = Request.Form["QuestionType" + i];

            aQuestionType.CreateDate = currentDate;

            questionTypeList.Add(aQuestionType);

            i++;
        }

        try
        {
            SiteHelper.AddQuestionTypeList(questionTypeList);

            statusMessage = questionTypeList.Count + " Type saved Successfully!";

            isValid = true;

        }
        catch (Exception ex)
        {
            isValid = false;

            statusMessage = "Error!\n" + ex.Message;
        }
    }

    for (i = 0; i < WebSite.OptionPerQuestion; i++)
    {
        QuestionType aQuestionType = new QuestionType();

        aQuestionType.QType = "";

        questionTypeList.Add(aQuestionType);
    }

    var allQType = SiteHelper.GetQTypeList();

    for (i = 1; i <= WebSite.OptionPerQuestion; i++)
    {
        Validation.RequireField("QuestionType" + i, "You must specify this Question Type or Remove.");
    }
}
@* If at least one validation error exists, notify the user *@
@if (!statusMessage.IsEmpty())
{
    if (isValid)
    {
    <p class="message success">
        @statusMessage
    </p>
    }
    else
    {
    <p class="message error">
        @statusMessage
    </p>
    }

}
<form method="post" action="">
<fieldset>
    <legend>Question Type Adding Form</legend>
    <div class="divwrapper">
        <div class="divwrapper">
            <div class="divwrapper" style="width: 70%; float: left;">
                <div class="question-area divwrapper">
                    <table id='TextBoxesGroup'>
                        @for (i = 0; i < WebSite.OptionPerQuestion; i++)
                        {
                            int increment = i + 1;

                            string idOrName = "QuestionType" + increment;

                            string trId = "TextBoxDiv" + increment;

                            <tr id="@trId">
                                <td>
                                    <label>
                                        Question Type # @increment:
                                    </label>
                                    <input type="text" id="@idOrName" name="@idOrName" title="Question Type @increment" placeholder="Question Type @increment" class="@SiteValidation.GetValidationClass(@idOrName)" @Validation.For(@idOrName) value="@questionTypeList[i].QType" />
                                    @Html.ValidationMessage(@idOrName)
                                </td>
                            </tr>
                        }
                    </table>
                    <input class="button thin gray" type='button' value='Add QuestionType' id='addButton' />
                    <input class="button thin gray" type='button' value='Remove QuestionType' id='removeButton' />
                </div>
            </div>
            <div class="divwrapper" style="width: 28%; float: right;">
                <label>
                    Current Question Type</label>
                <ul>
                    @{
                        foreach (var item in allQType)
                        {
                        <li>@item.QType</li>
                        }
                    }
                </ul>
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="divwrapper">
            <p class="form-actions">
                <input class="button medium green" type="submit" value="Save" title="Add Question" />
            </p>
        </div>
    </div>
</fieldset>
<script src="@Href("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery-1.3.2.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var counter = 5;
        $("#addButton").click(function () {
            if (counter>= 5) {
                alert("Maximum " + counter + " Question Types allow at a Time");
                return false;
            }

            counter++;

            var newTextBoxDiv = $(document.createElement('tr')).attr("id", 'TextBoxDiv' + counter);

            newTextBoxDiv.after().html('<td><label>Question Type #' + counter +
                 ' : </label>' + '<input type="text" id="QuestionType' + counter +
                 '" name="QuestionType' + counter +
                 '" title="QuestionType ' + counter +
                 '" placeholder="QuestionType ' + counter +
                 '" class="input-validation-valid" data-val-required="You must specify this QuestionType or Remove." data-val="true"><br /><span class="field-validation-valid" data-valmsg-for="QuestionType' + counter +
                 '" data-valmsg-replace="true"></span> </td>');

            newTextBoxDiv.appendTo("#TextBoxesGroup");
        });

        $("#removeButton").click(function () {
            if (counter == 1) {
                alert("Minimum 1 QuestionType must be");
                return false;
            }
            $("#TextBoxDiv" + counter).remove();
            counter--;
        });
    });
</script>
</form>